package thread;

public class SyncDemo3 {
    public static void main(String[] args) {
        // Thread t1=new Thread(Boo::doSome);
        //Thread t2=new Thread(Boo::doSome);
        //效果一样,写法不同
        Boo b1 = new Boo();
        Boo b2 = new Boo();
        Thread t1 = new Thread(() -> b1.doSome());
        Thread t2 = new Thread(() -> b2.doSome());

        t1.start();
        t2.start();

    }


}

class Boo {
    //写法一
   //public synchronized static void doSome() {
   public static void doSome() {
       //如果在静态方法使用同步块,则同步监视器是当前类的字节码对象
       //类对象的获取方式:类名.class
    synchronized (Boo.class){
        Thread t = Thread.currentThread();
        System.out.println(t.getName() + ":正在执行doSome方法...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {

        }
        System.out.println(t.getName() + ":正在执行doSome方法...");
    }
    }
}